package cn.tedu.carehome.foundation.controller;

import cn.tedu.carehome.foundation.pojo.dto.VisitorAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.VisitorUpdateDTO;
import cn.tedu.carehome.foundation.pojo.vo.VisitorListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.VisitorStandardVO;
import cn.tedu.carehome.foundation.service.IVisitorService;
import cn.tedu.carehome.foundation.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/visitors")
@Api(tags = "8.来访人员管理")
public class VisitorController {
    @Autowired
    private IVisitorService visitorService;
    @PostMapping("/add-new")
    @ApiOperation("添加来访人员")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(VisitorAddNewDTO visitorAddNewDTO){
        log.debug("开始处理添加来访人员的请求");
        visitorService.addNew(visitorAddNewDTO);
        return JsonResult.ok();
    }
    @GetMapping("/{id:\\d+}/delete")
    @ApiOperation("删除来访人员")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> deleteById(@PathVariable("id") Long id){
        log.debug("开始处理通过来访人员删除来访人员的请求");
        visitorService.deleteById(id);
       return JsonResult.ok();
    }
    @PostMapping("/deleteByIds")
    @ApiOperation("批量删除来访人员")
    @ApiOperationSupport(order = 210)
    public JsonResult<Void> deleteByIds(@RequestBody  Long[] ids){
        log.debug("开始处理通过来访人员ID批量删除来访人员的请求");
        visitorService.deleteByIds(ids);
        return JsonResult.ok();
    }
    @PostMapping("/{id:\\d+}/update")
    @ApiOperation("修改来访人员信息")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> Update(@PathVariable("id") Long id, VisitorUpdateDTO visitorUpdateDTO) {
        log.debug("开始处理修改来访人员数据详情请求，参数Id:{},新数据:{}",id,visitorUpdateDTO);
        visitorService.UpdateInfoById(id,visitorUpdateDTO);
        log.debug("修改成功！！！");
        return JsonResult.ok();
    }

    @GetMapping("/{oldId:\\d+}/count")
    @ApiOperation("查询来访人员数量")
    @ApiOperationSupport(order = 400)
    public JsonResult<Integer> selectByOldIdCount(@PathVariable("oldId") Long oldId){
        log.debug("开始处理查询来访人员数量的请求，参数ID:{}",oldId);
        Integer count = visitorService.count(oldId);
        return JsonResult.ok(count);
    }


    @GetMapping("/{id:\\d+}")
    @ApiOperation("查询来访人员详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<VisitorStandardVO> selectById(@PathVariable("id") Long id){
        log.debug("开始处理查询来访人员详情的请求，参数ID:{}",id);
        VisitorStandardVO visitorStandardVO = visitorService.selectById(id);
        return JsonResult.ok(visitorStandardVO);
    }
    @GetMapping("/list")
    @ApiOperation("查询来访人员列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<VisitorListItemVO>> listOldId(Integer page, Integer size){

        List<VisitorListItemVO> list = visitorService.list(page,size);
        return JsonResult.ok(list);
    }
    @GetMapping("/redis")
    @ApiOperation("重建来访人员缓存")
    @ApiOperationSupport(order = 500)
    public JsonResult<Void> redisOldId(Integer page, Integer size, Long oldId){
        log.debug("开始处理【重建来访人员缓存】的请求，参数page:{},参数size:{}");
        visitorService.rebuildCache(page,size);
        return JsonResult.ok();
    }
}
